package co.fitstart.mobile.web.controller.course;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.demo2do.core.wechat.web.interceptor.WechatJsConfig;

import co.fitstart.entity.course.round.CourseRound;
import co.fitstart.entity.courseware.fitnessgroup.BatchCourseware;
import co.fitstart.entity.courseware.fitnessgroup.BatchCoursewareType;
import co.fitstart.entity.courseware.fitnessgroup.DailyCourseware;
import co.fitstart.entity.user.User;
import co.fitstart.mobile.service.CourseRoundService;
import co.fitstart.mobile.service.CoursewareService;
import co.fitstart.mobile.web.interceptor.MobileDetect;
import co.fitstart.mobile.web.interceptor.Router;
import co.fitstart.mobile.web.interceptor.WechatAuthority;
import co.fitstart.mobile.web.resolver.Secure;

/**
 * 
 * @author mohanlan
 *
 */
@Controller
@RequestMapping("/profile/courses/{courseId}")
public class WeekCourswareController {
    
    @Autowired
    private CourseRoundService courseRoundService;
    
    @Autowired
    private CoursewareService coursewareService;
    
    /**
     * 
     * @param user
     * @param courseRoundId
     * @return
     */
    @RequestMapping("/weekly-coursewares")
    @Router(targetUrl = "/required-wechat")
    @WechatAuthority
    @MobileDetect
    @WechatJsConfig
    public ModelAndView list(@Secure User user,
                             @PathVariable("courseId") Long courseRoundId) {
        
        ModelAndView modelAndView = new ModelAndView("course/courseware/weekly-courseware-list");
        
        CourseRound courseRound = courseRoundService.loadCourseRound(courseRoundId);
        modelAndView.addObject("courseRound", courseRound);
        
        List<BatchCourseware> batchCoursewares = coursewareService.listGeneratedCourseware(courseRound, BatchCoursewareType.WEEKLY);
        modelAndView.addObject("batchCoursewares", batchCoursewares);
        
        return modelAndView;
    }
    
    /**
     * 
     * @param user
     * @param courseRoundId
     * @param batchId
     * @return
     */
    @RequestMapping("/weekly-coursewares/{batchId}")
    @Router(targetUrl = "/required-wechat")
    @WechatAuthority
    @MobileDetect
    @WechatJsConfig
    public ModelAndView detail(@Secure User user,
                               @PathVariable("courseId") Long courseRoundId,
                               @PathVariable("batchId") Long batchId) {
        
        ModelAndView modelAndView = new ModelAndView("course/courseware/weekly-courseware-detail");
        
        BatchCourseware batchCourseware = coursewareService.loadBatchCourseware(batchId);
        modelAndView.addObject("batchCourseware", batchCourseware);
        modelAndView.addObject("courseId", courseRoundId);
        
        return modelAndView;
    }
    
    /**
     * 
     * @param user
     * @param courseRoundId
     * @param batchId
     * @param dailyId
     * @return
     */
    @RequestMapping("/weekly-coursewares/{batchId}/daily-coursewares/{dailyId}")
    @Router(targetUrl = "/required-wechat")
    @WechatAuthority
    @MobileDetect
    @WechatJsConfig
    public ModelAndView dailyDetail(@Secure User user,
                                    @PathVariable("courseId") Long courseRoundId,
                                    @PathVariable("batchId") Long batchId,
                                    @PathVariable("dailyId") Long dailyId) {
        
        ModelAndView modelAndView = new ModelAndView("course/courseware/weekly-daily-courseware-detail");
        
        DailyCourseware dailyCourseware = coursewareService.loadDailyCourseware(dailyId);
        modelAndView.addObject("dailyCourseware", dailyCourseware);
        
        CourseRound courseRound = courseRoundService.loadCourseRound(courseRoundId);
        if(courseRound != null) {
            modelAndView.addObject("online", courseRound.getCourseType().isOnline());
        }
        
        return modelAndView;
    }
}
